//
//  FifthViewOfUploadPictureViewController.m
//  A段项目
//
//  Created by dllo on 16/3/12.
//  Copyright © 2016年 Scott. All rights reserved.
//

#import "FifthViewOfUploadPictureViewController.h"

#import "DeleUploadImage.h"
#import  <MBProgressHUD.h>
#import "HomePageCell.h"
#import "ChooseLocalPhotoLibView.h"

#import <DKNightVersion.h>

@interface FifthViewOfUploadPictureViewController ()<UITextViewDelegate,UIImagePickerControllerDelegate,UINavigationControllerDelegate,UICollectionViewDelegate,UICollectionViewDataSource,DeleteImageDelegate>

@property (nonatomic, retain) UITextView *textView;


@end

@implementation FifthViewOfUploadPictureViewController


{
    BOOL state; // 表示选中状态.
}

- (void)dealloc {
    
    [_textView release];
    
    [super dealloc];
    
}



- (void)viewDidLoad {
    [super viewDidLoad];
    
    
        
    [self.navigationController.navigationBar.backItem setTitle:@"取消"];
    
    self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc] initWithTitle:@"完成" style:0 target:self action:@selector(finishAction)];
    
    [self.navigationItem.rightBarButtonItem setTitle:@"完成"];
    
    [self createtextView];
    
    [self createCollectionView];
    
    [self.navigationItem.rightBarButtonItem release];

}

- (void)createCollectionView {
    
    UICollectionViewFlowLayout *layout = [[UICollectionViewFlowLayout alloc] init];
    
    layout.itemSize = CGSizeMake(80, 80);
    
    layout.minimumInteritemSpacing = 10;
    
    
    self.upLoadCollectionView = [[UICollectionView alloc] initWithFrame:CGRectMake(5, 220, self.view.frame.size.width - 10,self.view.frame.size.height - 220) collectionViewLayout:layout];
    
    self.upLoadCollectionView.backgroundColor = [UIColor whiteColor];
    
    [self.view addSubview:self.upLoadCollectionView];
    
    self.upLoadCollectionView.delegate = self;
    
    self.upLoadCollectionView.dataSource = self;
    
    [self.upLoadCollectionView registerClass:[HomePageCell class] forCellWithReuseIdentifier:@"pool1"];
    
    [_upLoadCollectionView release];
    
    [layout release];
    
    
}

- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section {
    
    
    
    return self.arrImage.count;
}


- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {
    
    HomePageCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"pool1" forIndexPath:indexPath];
    
    UIImage *image = [self.arrImage objectAtIndex:indexPath.item];
    
    cell.imageView.image = image;
    
    return cell;
    
}


/**点击完成是调用此方法. */
- (void)finishAction {
    
    /**当没有图片时走这个方法. */
    if (state) {
        
        UIAlertController *alert = [UIAlertController alertControllerWithTitle:@"上传图片" message:@"请选择一张图片" preferredStyle:1];
        
        UIAlertAction *action = [UIAlertAction actionWithTitle:@"我知道了" style:0 handler:^(UIAlertAction * _Nonnull action) {
            
            [alert popoverPresentationController];
            
        }];
        
        [alert addAction:action];
        
        [self presentViewController:alert animated:YES completion:^{
            
        }];
        
    }else {
        
        MBProgressHUD *hud = [MBProgressHUD showHUDAddedTo:self.view animated:YES];
        dispatch_async(dispatch_get_global_queue( DISPATCH_QUEUE_PRIORITY_LOW, 0), ^{
            
            //在此处写上传的代码.
            
            dispatch_async(dispatch_get_main_queue(), ^{
                
                
                hud.customView = [[[UIImageView alloc] initWithImage:[UIImage imageNamed:@"37x-Checkmark.png"]] autorelease];
                
                hud.mode = MBProgressHUDModeCustomView;
                
                hud.labelText = @"上传成功";
                
                [hud show:YES];
                
                [hud hide:YES afterDelay:1];
                
                [self.navigationController popViewControllerAnimated:YES];
                
            });
            
        });
        
        
    }
    
}


#pragma mark --textView

/**创建 textView. */
- (void)createtextView {
    
    
    self.textView = [[UITextView alloc] initWithFrame:CGRectMake(0, 64, self.view.frame.size.width, 150) textContainer:nil];
    
    self.textView.delegate = self;
    
    self.textView.text = @"图片描述";
    self.textView.textColor = [UIColor lightGrayColor];
    
    self.automaticallyAdjustsScrollViewInsets = NO;
    
    [self.view addSubview:self.textView];
    
    self.textView.contentSize = CGSizeMake(self.view.frame.size.width, 0);
    
    [_textView release];
    
}


- (void)textViewDidBeginEditing:(UITextView *)textView {
    
    
    self.textView.text = @""; //开始编辑时 清空里面的内容.
    self.textView.textColor = [UIColor blackColor];
    
    
}

/**点击空白处回收键盘. */

- (void)touchesEnded:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event {
    
    [self.textView endEditing:YES];
    
    
}

- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath {
    
    
    if (indexPath.item == self.arrImage.count - 1) {
        
        CATransition *transition = [CATransition animation];
        transition.type = kCATransitionPush;//可更改为其他方式
        transition.subtype = kCATransitionFromTop;//可更改为其他方式
        [self.navigationController.view.layer addAnimation:transition forKey:kCATransition];
        

        [self.navigationController popViewControllerAnimated:YES];
        
     
        
        self.navigationController.modalTransitionStyle = 2;
        
        
    }else {
        
            DeleUploadImage *dele = [[DeleUploadImage alloc] init];
            
            dele.image = [self.arrImage objectAtIndex:indexPath.item];
            
            dele.delegate = self;  //设置代理删除图片.
            
            [self presentViewController:dele animated:YES completion:^{
                
            }];
            
            [dele release];

    }
    
    
}

- (void)deleteImage:(UIImage *)image {
    
    [self.arrImage removeObject:image];
    
    [self.upLoadCollectionView reloadData];
    
    
}

- (void)viewWillAppear:(BOOL)animated {
    
    if ([[NSUserDefaults standardUserDefaults] boolForKey:@"夜间模式"]) {
        
        [DKNightVersionManager nightFalling];
        
    } else {
        
        [DKNightVersionManager dawnComing];
    }

    self.view.dk_backgroundColorPicker = DKColorWithColors([UIColor whiteColor],[UIColor colorWithRed:52 / 256.f green:52 / 256.f blue:52 / 256.f alpha:1]);
    
    self.textView.dk_backgroundColorPicker = DKColorWithColors([UIColor whiteColor],[UIColor colorWithRed:52 / 256.f green:52 / 256.f blue:52 / 256.f alpha:1]);
    
    self.textView.dk_textColorPicker = DKColorWithColors([UIColor blackColor], [UIColor whiteColor]);
    
    self.upLoadCollectionView.dk_backgroundColorPicker =  DKColorWithColors([UIColor whiteColor],[UIColor colorWithRed:52 / 256.f green:52 / 256.f blue:52 / 256.f alpha:1]);
    
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
